// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_rag_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type BasicRagConfigDbItem struct {
	Name         string                            `bson:"name"`
	ProviderType roadmap_rag_api.RAG_PROVIDER_TYPE `bson:"providerType"`
	BaseApiAddr  string                            `bson:"baseApiAddr"`
	ApiKey       string                            `bson:"apiKey"`
	AgentId      string                            `bson:"agentId"`
}

type RagConfigDbItem struct {
	RagConfigId string               `bson:"_id"`
	RoadmapId   string               `bson:"roadmapId"`
	BasicConfig BasicRagConfigDbItem `bson:"basicConfig"`
	CreateTime  int64                `bson:"createTime"`
	UpdateTime  int64                `bson:"updateTime"`
}

func (item *RagConfigDbItem) ToRagConfig() *roadmap_rag_api.RagConfig {
	return &roadmap_rag_api.RagConfig{
		RagConfigId: item.RagConfigId,
		BasicConfig: &roadmap_rag_api.BasicRagConfig{
			Name:         item.BasicConfig.Name,
			ProviderType: item.BasicConfig.ProviderType,
			BaseApiAddr:  item.BasicConfig.BaseApiAddr,
			ApiKey:       item.BasicConfig.ApiKey,
			AgentId:      item.BasicConfig.AgentId,
		},
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _RagConfigDao struct {
	mc *mongo.Collection
}

func newRagConfigDao(db *mongo.Database) (*_RagConfigDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "rag_config")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"roadmapId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_RagConfigDao{mc: mc}, nil
}

func (db *_RagConfigDao) Insert(ctx context.Context, dbItem *RagConfigDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_RagConfigDao) Exist(ctx context.Context, roadmapId, ragConfigId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"roadmapId": roadmapId, "_id": ragConfigId})
	return count > 0, err
}

func (db *_RagConfigDao) Get(ctx context.Context, roadmapId, ragConfigId string) (*RagConfigDbItem, error) {
	retItem := &RagConfigDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"roadmapId": roadmapId, "_id": ragConfigId}).Decode(retItem)
	return retItem, err
}

func (db *_RagConfigDao) Remove(ctx context.Context, roadmapId, ragConfigId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"roadmapId": roadmapId, "_id": ragConfigId})
	return err
}

func (db *_RagConfigDao) RemoveByRoadmap(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId})
	return err
}

func (db *_RagConfigDao) List(ctx context.Context, roadmapId string) ([]*RagConfigDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"roadmapId": roadmapId}, options.Find().SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*RagConfigDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_RagConfigDao) UpdateBasic(ctx context.Context, roadmapId, ragConfigId string, basicItem *BasicRagConfigDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"roadmapId": roadmapId, "_id": ragConfigId}, bson.M{"$set": bson.M{
		"basicConfig": basicItem,
		"updateTime":  updateTime,
	}})
	return err
}
